/**
 * 流程定义工具类
 * @returns
 */

//获取随机节点id
export function getRandNodeId() {
  //前缀node_ + 时间戳 + 4位随机数
  return `${new Date().getTime()}${Math.floor(Math.random() * 10000)
    .toString()
    .padStart(4, "0")}`
}

export function generateNumber(len: number) {
  //前缀node_ + 时间戳 + 4位随机数
  let result = ""
  const characters = "0123456789"
  const charactersLength = characters.length
  for (let i = 0; i < len; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength))
  }
  return result
}

export function generateStr(len: number) {
  let result = ""
  const characters = "abcdefghijklmnopqrstuvwxyz0123456789"
  const charactersLength = characters.length
  for (let i = 0; i < len; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength))
  }
  return result
}

export function deepCopy(obj) {
  return JSON.parse(JSON.stringify(obj))
}

export function delField(cols, i) {
  cols.splice(i, 1)
}

export function copyField(cols, i) {
  const col = deepCopy(cols[i])
  col.id = col.type + getRandNodeId()
  col.code = col.type + getRandNodeId()
  cols.push(col)
}

//重载所有的节点id
export function reloadNodeId(nodes) {
  if (Array.isArray(nodes)) {
    nodes.forEach(node => {
      if (node.type === "GATEWAY") {
        node.id = getRandNodeId()
        //分支头部节点
        reloadNodeId(node.props.branch)
        //分支
        reloadNodeId(node.branch)
      } else {
        node.id = getRandNodeId()
      }
    })
  } else {
    nodes.id = getRandNodeId()
  }
}
